package vn.gq.tym.model.bean;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class BookmarkItem implements Parcelable {
	public String catName = "";
	public String catUrl = "";
	public String chapterName = "";
	public int chapterPos = 0;
	public int scrollPosY = 0;
	
	public BookmarkItem() {
		
	}
	
	public BookmarkItem(Parcel in) {
		catUrl = in.readString();
		catName = in.readString();
		chapterName = in.readString();
		chapterPos = in.readInt();
		scrollPosY = in.readInt();
	}
	
	public BookmarkItem(String json) {
		JSONObject in;
		try {
			if (json == null) json = "";
			in = new JSONObject(json);
			catName = in.getString("catName");
			catUrl = in.getString("catId");
			chapterName = in.getString("chapterName");
			chapterPos = in.getInt("chapterPos");
			scrollPosY = in.getInt("scrollPosY");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public void clear() {
		scrollPosY = 0;
	}
	
	public void setCatItem(CategoryItem cat) {
		catUrl = cat.getUrl();
		catName = cat.getTitle();
	}
	
	public JSONObject toJSONObject() {
		JSONObject output = new JSONObject();
		try {
			output.put("catName", catName);
			output.put("catId", catUrl);
			output.put("chapterName", chapterName);
			output.put("chapterPos", chapterPos);
			output.put("scrollPosY", scrollPosY);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return output;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(catUrl);
		dest.writeString(catName);
		dest.writeString(chapterName);
		dest.writeInt(chapterPos);
		dest.writeInt(scrollPosY);
	}
	
	public static final Parcelable.Creator<BookmarkItem> CREATOR = new Parcelable.Creator<BookmarkItem>() {

		@Override
		public BookmarkItem createFromParcel(Parcel source) {
			return new BookmarkItem(source);
		}

		@Override
		public BookmarkItem[] newArray(int size) {
			return new BookmarkItem[size];
		}
	};
	
}
